package com.smyh.model.host.param;

import android.content.Context;
import android.graphics.Color;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.smyh.UIM.host.param.Version_info_Param;
import com.smyh.bean.SoftVersionInfo;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.model.tableModel.KtableView;

/**
 * Created by 姜龙 on 2016/7/8.
 */
public class VersionInfoModel {

    private static TableLayout tableLayout;

    public static TableLayout getTableLayout(Context context){
        if(tableLayout == null){
            tableLayout = new TableLayout(context);
            tableLayout.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
            tableLayout.setBackgroundColor(Color.BLACK);
            KtableView.setIsEditColNum(1);
            String header0[] = {"版本信息"};
            String header1[] = {"名称","值","描述"};
            String test1[] = {"软件版本","","3字节"};
            String test2[] = {"卡ip地址","","4字节"};
            String test3[] = {"信号强度","","1字节"};
            tableLayout.addView(KtableView.addTableHeaders(context,header0));
            tableLayout.addView(KtableView.addTableHeaders(context,header1));
            tableLayout.addView(KtableView.addNewRow(context,test1));
            tableLayout.addView(KtableView.addNewRow(context,test2));
            tableLayout.addView(KtableView.addNewRow(context,test3));
        }
        return tableLayout;
    }

    public static void setTableLayout() {

        for (int i = 2; i < tableLayout.getChildCount(); i++) {
            TableRow childRow = (TableRow) tableLayout.getChildAt(i);
            SoftVersionInfo info = Version_info_Param.softVersionInfoMap.get(MainCahce.chooseDeviceAddr);
            if(info != null){

                switch (i) {
                    case 2:
                        Integer[] softVersionArray = info.getSoftVersionArray();
                        String data1Str = "" ;
                        try{
                            data1Str = "" + softVersionArray[0] + "." + softVersionArray[1] +
                                    "." + softVersionArray[2];
                        }catch (Exception e) {
                            data1Str = "";
                        }
                        ((TextView) childRow.getChildAt(1)).setText(data1Str);
                        break;
                    case 3:
                        Integer[] ipAddrForCardArray = info.getIpAddrForCardArray();
                        String data2Str = "" ;
                        try{
                            data2Str = "" + ipAddrForCardArray[0] + "." + ipAddrForCardArray[1] +
                                    "." + ipAddrForCardArray[2] + "." + ipAddrForCardArray[3];
                        }catch (Exception e) {
                            data2Str = "";
                        }
                        ((TextView) childRow.getChildAt(1)).setText(data2Str);
                        break;
                    case 4:
                        ((TextView) childRow.getChildAt(1)).setText(info.getSingalGrade() + "");
                        break;
                }
            }else {

                switch (i) {
                    case 2:
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        break;
                    case 3:
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        break;
                    case 4:
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        break;
                }
            }
        }
    }

}
